package com.dhecp.project.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.enums.DataLimitType;
import com.dhecp.core.enums.PageTemplate;
import com.dhecp.project.system.domain.SysRoleInfo;
import com.dhecp.project.system.service.SysRoleInfoService;

/**
 * 角色信息 控制层
 * @author DH-Xxj
 * @version 2019年10月18日
 */

@Controller
@RequestMapping("/system/role")
public class SysRoleInfoController extends BaseController {
    private String prefix = "system/role";
    private String moduleTable = ModuleTable.ROLE_INFO;

    @Autowired
    private SysRoleInfoService roleInfoService;
    
    /**
     * 打开角色列表
     * @param 
     * @return ModelAndView
     * @author DH-Xxj
     */
    @RequiresPermissions("system:role:view")
    @GetMapping()
    public ModelAndView userInfoList(@RequestParam Map<String, Object> params){
        ModelAndView mv = this.getModelAndView();
//        枚举类型转为Map格式
//        Map<String,Object> dataLimitMap = new LinkedHashMap<String,Object>();
//        DataLimitType[] dataLimitTypeList = DataLimitType.values();
//        for (int i = 0; i < dataLimitTypeList.length; i++){
//        	dataLimitMap.put(dataLimitTypeList[i].getCode(), dataLimitTypeList[i].getInfo());
//        }
        Map<String,Object> dataLimitMap = TypeUtils.EnumToMap(DataLimitType.class);
        Map<String,Object> pageTemplateMap = TypeUtils.EnumToMap(PageTemplate.class);
        dataLimitMap.remove("OperateData"); //去除运营公司权限
        
        mv.addObject("dataLimitMap", dataLimitMap); //数据权限字段下拉框数据
        mv.addObject("pageTemplateMap", pageTemplateMap); //首页模板列表
        mv.addObject("moduleTable", moduleTable); //数据权限字段下拉框数据
        mv.setViewName(prefix + "/role");
        return mv;
    }
    
    /**
     * 打开角色功能权限分配页面
     * @param  guid 必要参数：角色唯一值
     * @return ModelAndView
     * @author DH-Xxj
     */
    @RequiresPermissions(value={"system:role:view","system:role:edit"},logical=Logical.AND)
    @GetMapping("/userandrolejurisdictionformodule")
    public ModelAndView userAndRoleJurisdictionForModuleEdit(@RequestParam Map<String, Object> params){
        ModelAndView mv = this.getModelAndView();
        mv = this.gotoModelAndViewErrorPage(params, "guid", "角色唯一值【Guid】");
        if (mv.isEmpty()){            
            mv.addObject("vType", "role");	//role:角色权限分配,user:用户权限分配
            mv.addObject("pid", params.get("guid"));	//分配权限的角色
            mv.setViewName("system/userandrolejurisdiction/userandrolejurisdictionformodule_edit");        	
        }
        return mv;
    }
    
    /**
     * 打开角色数据权限分配页面
     * @param  guid 必要参数：角色唯一值
     * @return ModelAndView
     * @author DH-Xxj
     */
    @RequiresPermissions(value={"system:role:view","system:role:edit"},logical=Logical.AND)
    @GetMapping("/userandrolejurisdictionfordata")
    public ModelAndView userAndRoleJurisdictionForDataEdit(@RequestParam Map<String, Object> params){
        ModelAndView mv = this.getModelAndView();
        mv = this.gotoModelAndViewErrorPage(params, "guid", "角色唯一值【Guid】");
        if (mv.isEmpty()){
            Map<String,Object> dataLimitMap = TypeUtils.EnumToMap(DataLimitType.class);
            dataLimitMap.remove("OperateData"); //去除运营公司权限
            
            mv.addObject("dataLimitMap", dataLimitMap); //查询条件下拉框数据
            mv.addObject("vType", "role");	//role:角色权限分配,user:用户权限分配
            mv.addObject("pid", params.get("guid"));	//分配权限的角色
            mv.setViewName("system/userandrolejurisdiction/userandrolejurisdictionfordata_edit");        	
        }
        return mv;
    }
    /**
     * 打开角色选择页面
     * @param params
     * @return
     * @author kimiloo 
     */
//	@RequiresPermissions("system:role:view")
    @GetMapping("/select")
    public ModelAndView selectPage(@RequestParam Map<String, Object> params){
		ModelAndView mv = this.getModelAndView();
        mv.addObject("pagetype", params.get("pagetype").toString());
        mv.setViewName(prefix + "/role_select");
		return mv;
    }
    /**
     * 按条件查询角色信息
     * @param params
     * @return TableDataInfo
     * @author DH-Xxj
     */
//    @RequiresPermissions("system:role:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
        Map<String,Object> dataLimitMap = TypeUtils.EnumToMap(DataLimitType.class);
        dataLimitMap.remove("OperateData"); //去除运营公司权限
        List<SysRoleInfo> list = new ArrayList<SysRoleInfo>();
        try {
            list = roleInfoService.queryRoleInfoList(params);
            for (SysRoleInfo roleInfo : list){
            	if (dataLimitMap.get(roleInfo.getRoleDataLimit()) != null){
                	roleInfo.setRoleDataLimit(dataLimitMap.get(roleInfo.getRoleDataLimit()).toString());
            	}
            }
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list);
    }

    /**
     * 新增角色——数据提交
     * @param roleInfoItems 必要参数：对应实体类的json格式字符串
     * @return AjaxResult
     * @author DH-Xxj
     */
    @RequiresPermissions("system:role:add")
    @Log(title = "新增角色", businessType = BusinessType.INSERT)
    @PostMapping("/addpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult roleInfoAddPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        if (params.get("roleInfoItems") == null || params.get("roleInfoItems") == ""){
        	return AjaxResult.error("缺少参数roleInfoItems【对应实体类的json格式字符串】。");
        }
        try {
            ajaxResult= roleInfoService.insertRoleInfo(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 删除角色——数据提交
     * @param roleInfoItems 必要参数：对应实体类的json格式字符串
     * @return AjaxResult
     * @author DH-Xxj
     */
    @RequiresPermissions("system:role:delete")
    @Log(title = "删除角色", businessType = BusinessType.DELETE)
    @PostMapping("/deletepost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult roleInfoDeletePost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        if (params.get("roleInfoItems") == null || params.get("roleInfoItems") == ""){
        	return AjaxResult.error("缺少参数roleInfoItems【对应实体类的json格式字符串】。");
        }
        try {
            ajaxResult= roleInfoService.deleteRoleInfo(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 修改角色信息——数据提交
     * @param roleInfoItems 必要参数：对应实体类的json格式字符串
     * @return AjaxResult
     * @author DH-Xxj
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "修改角色", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult roleInfoEditPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        if (params.get("roleInfoItems") == null || params.get("roleInfoItems") == ""){
        	return AjaxResult.error("缺少参数roleInfoItems【对应实体类的json格式字符串】。");
        }
        try {
            ajaxResult= roleInfoService.updateRoleInfo(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
}
